<?php

require_once('./fpdf/tcpdf/tcpdf.php');

// create new PDF document
//$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf = new TCPDF($orientation = 'P', $unit = 'mm', $format = 'A4', $unicode = true, $encoding = 'UTF-8', $diskcache = false, $pdfa = false);

/*
  // set document information
  $pdf->SetCreator(PDF_CREATOR);
  $pdf->SetAuthor('Nicola Asuni');
  $pdf->SetTitle('TCPDF Example 048');
  $pdf->SetSubject('TCPDF Tutorial');
  $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
 */
// set default header data
//$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 04', PDF_HEADER_STRING);
$pdf->SetHeaderData("", "", "รายงานการหักภาษี ณ ที่จ่าย", "");

// set header and footer fonts
//$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setHeaderFont(Array("thsarabunb", 'B', 22));

$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
//$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language-dependent strings (optional)
// ---------------------------------------------------------
// set font
//$pdf->SetFont('helvetica', 'B', 20);
$pdf->SetFont('thsarabunb', 'B', 22);

// add a page
$pdf->AddPage();

//$pdf->Write(0, 'รายงานการจัดเช็ค', '', 0, 'L', true, 0, false, false, 0);
//$pdf->SetFont('helvetica', '', 8);
// -----------------------------------------------------------------------------


$pdf->SetFont('thsarabunb', 'B', 16);
$tbl_header = '
<table cellspacing="0" cellpadding="1" border="1" width="100%">
<tr align="center">
    <th>#</th>
    <th>วันที่</th>
    <th>ผู้ถูกหักภาษี</th>
    <th>เลขที่ฎีกา</th>
    <th>ภาษีหัก ณ ที่จ่าย</th>
</tr>
';

require_once("includes/connect.php");
require_once("includes/function.php");

$db = new myDBS;

$sql = "SELECT approved_date, receiver, pet_id, tax1, amount "
        . " FROM tbl_pet "
        . " WHERE approved_status = 'อนุมัติ' "
        . " UNION ALL"
        . " SELECT approved_date, company_name AS receiver, p_id as pet_id, WithholdingTax AS tax1, inpValues AS amount"
        . " FROM tbl_petition "
        . " WHERE approved_status = 'อนุมัติ' ";

//echo $sql;
$result = $db->runSQL($sql);
$pdf->SetFont('thsarabunb', '', 16);
$i = 1;

while ($rows = $db->fetch_object($result)) {
    $date = date_create($rows->approved_date);
    
    $tbl_body = $tbl_body. "<tr>"
            . "<td>$i</td>"
            . "<td>".date_format($date, "d/m/Y")."</td>"
            . "<td>$rows->receiver</td>"
            . "<td>$rows->pet_id</td>"
            . "<td align='center'>"
            . "" . number_format($rows->amount * $rows->tax1 / 100, 2) . ""
            . "</td>"
            . "</tr>";

    $i++;
}

$tbl_footer = '</table>';

//echo $tbl_header.$tbl_body.$tbl_footer;

$pdf->writeHTML($tbl_header . $tbl_body . $tbl_footer, true, false, false, false, '');

//$pdf->Output('example_048.pdf', 'I');
$pdf->Output();
?>